//
//  MyPageViewController.m
//  YiLiao
//
//  Created by Infinite on 16/7/1.
//  Copyright © 2016年 Infinite. All rights reserved.
//

#import <AFNetworking.h>
#import "MyPageViewController.h"
#import "PITableViewCell.h"


@interface MyPageViewController ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{
    NSDictionary * MyPageDic ;
}
@property (nonatomic,strong) UITableView *tableView;


@end

@implementation MyPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
        self.view.backgroundColor=[UIColor grayColor];
    self.title=@"个人中心";
    self.tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:(UITableViewStyleGrouped)];
    _tableView.dataSource=self;
    _tableView.delegate=self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell1"];
    [_tableView registerNib:[UINib nibWithNibName:@"PITableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"PICell"];
    [self.view addSubview:_tableView];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 3;
    }else{
        return 1;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        PITableViewCell * cell0 = [tableView dequeueReusableCellWithIdentifier:@"PICell" forIndexPath:indexPath];
        cell0.titleImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[MyPageDic objectForKey:@"headerurl"]]]];
        cell0.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        return cell0;
    }else if (indexPath.section == 1){
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (indexPath.row==0) {
            cell.textLabel.text=@"清楚文件缓存";
        }else if (indexPath.row==1){
            cell.textLabel.text=@"清楚图像缓存";
        }else{
            cell.textLabel.text=@"退出登入";
        }
        return cell;
    }else{
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text=@"关于我们";
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 90;
    }
    return 50;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        UIImagePickerController * picker ;
        if (picker ==nil) {
            picker = [[UIImagePickerController alloc]init];
        }
        picker.delegate =self;
        picker.allowsEditing=YES;
      //判断用户手机是否支持该功能；
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:picker animated:YES completion:nil];
        }else{
            NSLog(@"请去设置允许操作");
        }
//                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
//                    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//                    [self presentViewController:picker animated:YES completion:nil];
//                }else{
//                    NSLog(@"请去设置允许操作");
//                }
        

    }
}
-(void)AFNetSession{
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"text/html",nil]];
    NSString * access_Token =[[NSUserDefaults standardUserDefaults]objectForKey:@"access_token"];
    
    
    NSDictionary * parameters =  @{@"access_token":access_Token,@"command":@"ST_GPI"};
    [manager POST:@"http://localhost:8080/st/s" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        MyPageDic=[responseObject objectForKey:@"data"];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
    }];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo{
    
    PITableViewCell * cell;
    
    cell.titleImageView.image=image;
    NSData *data = UIImagePNGRepresentation(image);
    
    //创建一个Post
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes=[manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:[NSSet setWithObjects:@"text/html",@"text/plain",nil]];
    NSString * access_Token =[[NSUserDefaults standardUserDefaults]objectForKey:@"access_token"];
    NSDictionary * parameters =  @{@"access_token":access_Token,@"command":@"ST_H"};
    [manager POST:@"http://IP:Port/st/s?command=ST_H&access_token=ACCESS_TOKEN" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        ///开始上传文件
        NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString * str = [formatter stringFromDate:[NSDate date]];
        NSString * fileName = [NSString stringWithFormat:@"%@.png",str];
        [formData appendPartWithFileData:data name:@"photos" fileName:fileName mimeType:@"image/png"];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"上传成功");
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"上传失败");
    }];
    
    
    





}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
